package com.cabrida.livestar.main;

import java.util.ArrayList;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONObject;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.cabrida.livestar.BaseActivity;
import com.cabrida.livestar.R;
import com.cabrida.livestar.config.Global;
import com.cabrida.livestar.config.HttpHelp;
import com.cabrida.livestar.config.Urls;
import com.cabrida.livestar.mainstar.CircleActivity;
import com.cabrida.livestar.model.FansCircleMsgChildrenData;
import com.cabrida.livestar.model.FansCircleMsgData;
import com.cabrida.livestar.model.JSONParser;
import com.cabrida.livestar.model.StarCircleData;
import com.cabrida.livestar.util.HttpUtils;
import com.cabrida.livestar.util.JsonUtils;
import com.cabrida.livestar.util.L;
import com.cabrida.livestar.util.MyUtils;
import com.cabrida.livestar.util.NetworkUtils;
import com.cabrida.livestar.view.CircleImageView;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

/**
 * 粉丝端-星友圈详情
 */
public class FansCircleActivity extends BaseActivity {
	public static final String TAG = FansCircleActivity.class.getSimpleName();
	private ImageView iv_back;
	private TextView tv_title;
	private ListView listView;
	private FansCircleMsgLvAdapter adapter;
	private StarCircleData circleData;
	private LinearLayout ll_onlive;
	private TextView tv_name;
	private TextView tv_time;
	private TextView tv_content;
	private TextView tv_like_num;
	private TextView tv_comment_num;
	private TextView tv_share_num;
	private LinearLayout ll_image_1;
	private LinearLayout ll_image_2;
	private LinearLayout ll_image_3;
	private ImageView image_1;
	private ImageView image_2;
	private ImageView image_3;
	private ImageView image_4;
	private ImageView image_5;
	private ImageView image_6;
	private ImageView image_7;
	private ImageView image_8;
	private ImageView image_9;
	private CircleImageView iv_header;
	private EditText et_content;
	private TextView tv_send;
	private LinearLayout ll_like;
	
	private String clickMsgCid="";
	private String clickMsgCudid="";
	private String clickMsgCnickname="";
	
	private BroadcastReceiver mReceiver = new BroadcastReceiver(){

		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction().equals(CircleActivity.ACTION_CHILDREN_MSG)) {
				FansCircleMsgChildrenData clickmsgData = (FansCircleMsgChildrenData) intent.getSerializableExtra("data");
				clickMsgCid = clickmsgData.getC_parent();
				clickMsgCudid = clickmsgData.getC_udid();
				clickMsgCnickname = clickmsgData.getCnickname();
				et_content.setText("回复"+clickMsgCnickname+":");
				et_content.setSelection(et_content.getText().length());
			}
		}
	};
	
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(mReceiver);
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		IntentFilter filter_ad = new IntentFilter();
		filter_ad.addAction(CircleActivity.ACTION_CHILDREN_MSG);
		registerReceiver(mReceiver, filter_ad);
	}
	
	/** 获取圈子评论数据 */
	private void getCircleMsgList() {
		// TODO Auto-generated method stub
		RequestParams params = new RequestParams();
		params.put("udid", Global.U_id);
		params.put("token", Global.U_token);
		params.put("cid", circleData.getCid());
		params.put("type", Global.MESSAGE_TYPE_2);
		params.put("page", 1);
		if (!NetworkUtils.isNetAvailable(context)) {
			showToast(R.string.no_network);
			return;
		}
		showProgress();
		HttpUtils.getClient().post(Urls.GET_STARCIRCLEMSG_URL, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						dismissProgress();
						L.e(TAG, "getCircleMsgList", response.toString());
						int code = JsonUtils.getInt(response, "code", -1);
						if (code == HttpHelp.Succeed) {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							JSONArray list = JsonUtils.getJSONArray(data, "list", new JSONArray());
							ArrayList<FansCircleMsgData> infoList = JSONParser.getFansCircleMsgList(list);
							adapter.setData(infoList);
							adapter.notifyDataSetChanged();
						} else {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							String message=JsonUtils.getString(data,"message", "");
							showToast(message);
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						dismissProgress();
						showToast(getResources().getString(R.string.fwq_error));
					}
				});
	}
	
	/** 点赞 */
	private void btnLike() {
		// TODO Auto-generated method stub
		RequestParams params = new RequestParams();
		params.put("udid", Global.U_id);
		params.put("token", Global.U_token);
		params.put("cid", circleData.getC_udid());
		if (!NetworkUtils.isNetAvailable(context)) {
			showToast(R.string.no_network);
			return;
		}
		showProgress();
		HttpUtils.getClient().post(Urls.GET_CIRCLE_LIKE_URL, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						dismissProgress();
						L.e(TAG, "btnLike", response.toString());
						int code = JsonUtils.getInt(response, "code", -1);
						if (code == HttpHelp.Succeed) {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							String message=JsonUtils.getString(data,"message", "");
							showToast(message);
							circleData.setC_like(JsonUtils.getString(data,"number", ""));
							tv_like_num.setText(circleData.getC_like().toString().trim());
						} else {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							String message=JsonUtils.getString(data,"message", "");
							showToast(message);
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						dismissProgress();
						showToast(getResources().getString(R.string.fwq_error));
					}
				});
	}
	
	/** 发评论 */
	private void sendContent(String description,String parentId,String pudid) {
		// TODO Auto-generated method stub
		RequestParams params = new RequestParams();
		params.put("udid", Global.U_id);
		params.put("token", Global.U_token);
		params.put("cid", circleData.getCid());
		params.put("type", Global.MESSAGE_TYPE_2);
		params.put("description", description);
		params.put("parent", parentId);
		params.put("pudid", pudid);
		if (!NetworkUtils.isNetAvailable(context)) {
			showToast(R.string.no_network);
			return;
		}
		showProgress();
		HttpUtils.getClient().post(Urls.GET_CIRCLE_SEND_CONTENT_URL, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						dismissProgress();
						L.e(TAG, "sendContent", response.toString());
						int code = JsonUtils.getInt(response, "code", -1);
						if (code == HttpHelp.Succeed) {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							String message=JsonUtils.getString(data,"message", "");
							showToast(message);
							et_content.setText("");
							getCircleMsgList();
						} else {
							JSONObject data = JsonUtils.getJSONObject(response, "data", new JSONObject());
							String message=JsonUtils.getString(data,"message", "");
							showToast(message);
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						dismissProgress();
						showToast(getResources().getString(R.string.fwq_error));
					}
				});
	}
	
	/**
	 * 填数据
	 */
	private void setData(){
		MyUtils.loadImg(circleData.getU_avatar(), iv_header);
		tv_name.setText(circleData.getU_nickname().toString());
		tv_time.setText(circleData.getC_createtime().toString());
		if (!TextUtils.isEmpty(circleData.getC_content())) {
			tv_content.setText(circleData.getC_content().toString());
		}else {
			tv_content.setText("");
		}
		tv_like_num.setText(circleData.getC_like().toString());
		tv_comment_num.setText(circleData.getC_comment().toString());
		tv_share_num.setText(circleData.getC_share().toString());
		
		ll_image_1.setVisibility(View.GONE);
		ll_image_2.setVisibility(View.GONE);
		ll_image_3.setVisibility(View.GONE);
		if (!TextUtils.isEmpty(circleData.getC_images())) {
			String strs[] = circleData.getC_images().split(",");
			switch (strs.length) {
			case 1:
				ll_image_1.setVisibility(View.VISIBLE);
				ll_image_2.setVisibility(View.GONE);
				ll_image_3.setVisibility(View.GONE);
				image_1.setVisibility(View.VISIBLE);
				image_2.setVisibility(View.INVISIBLE);
				image_3.setVisibility(View.INVISIBLE);
				MyUtils.loadImg(strs[0], image_1);
				break;
			case 2:
				ll_image_1.setVisibility(View.VISIBLE);
				ll_image_2.setVisibility(View.GONE);
				ll_image_3.setVisibility(View.GONE);
				image_1.setVisibility(View.VISIBLE);
				image_2.setVisibility(View.VISIBLE);
				image_3.setVisibility(View.INVISIBLE);
				MyUtils.loadImg(strs[0], image_1);
				MyUtils.loadImg(strs[1], image_2);
				break;
			case 3:
				ll_image_1.setVisibility(View.VISIBLE);
				ll_image_2.setVisibility(View.GONE);
				ll_image_3.setVisibility(View.GONE);
				image_1.setVisibility(View.VISIBLE);
				image_2.setVisibility(View.VISIBLE);
				image_3.setVisibility(View.VISIBLE);
				MyUtils.loadImg(strs[0], image_1);
				MyUtils.loadImg(strs[1], image_2);
				MyUtils.loadImg(strs[2], image_3);
				break;
			case 4:
				ll_image_1.setVisibility(View.VISIBLE);
				ll_image_2.setVisibility(View.VISIBLE);
				ll_image_3.setVisibility(View.GONE);
				image_1.setVisibility(View.VISIBLE);
				image_2.setVisibility(View.VISIBLE);
				image_3.setVisibility(View.INVISIBLE);
				image_4.setVisibility(View.VISIBLE);
				image_5.setVisibility(View.VISIBLE);
				image_6.setVisibility(View.INVISIBLE);
				MyUtils.loadImg(strs[0], image_1);
				MyUtils.loadImg(strs[1], image_2);
				MyUtils.loadImg(strs[2], image_4);
				MyUtils.loadImg(strs[3], image_5);
				break;
			case 5:
				ll_image_1.setVisibility(View.VISIBLE);
				ll_image_2.setVisibility(View.VISIBLE);
				ll_image_3.setVisibility(View.GONE);
				image_1.setVisibility(View.VISIBLE);
				image_2.setVisibility(View.VISIBLE);
				image_3.setVisibility(View.VISIBLE);
				image_4.setVisibility(View.VISIBLE);
				image_5.setVisibility(View.VISIBLE);
				image_6.setVisibility(View.INVISIBLE);
				MyUtils.loadImg(strs[0], image_1);
				MyUtils.loadImg(strs[1], image_2);
				MyUtils.loadImg(strs[2], image_3);
				MyUtils.loadImg(strs[3], image_4);
				MyUtils.loadImg(strs[4], image_5);
				break;
			case 6:
				ll_image_1.setVisibility(View.VISIBLE);
				ll_image_2.setVisibility(View.VISIBLE);
				ll_image_3.setVisibility(View.GONE);
				image_1.setVisibility(View.VISIBLE);
				image_2.setVisibility(View.VISIBLE);
				image_3.setVisibility(View.VISIBLE);
				image_4.setVisibility(View.VISIBLE);
				image_5.setVisibility(View.VISIBLE);
				image_6.setVisibility(View.VISIBLE);
				MyUtils.loadImg(strs[0], image_1);
				MyUtils.loadImg(strs[1], image_2);
				MyUtils.loadImg(strs[2], image_3);
				MyUtils.loadImg(strs[3], image_4);
				MyUtils.loadImg(strs[4], image_5);
				MyUtils.loadImg(strs[5], image_6);
				break;
			case 7:
				ll_image_1.setVisibility(View.VISIBLE);
				ll_image_2.setVisibility(View.VISIBLE);
				ll_image_3.setVisibility(View.VISIBLE);
				image_1.setVisibility(View.VISIBLE);
				image_2.setVisibility(View.VISIBLE);
				image_3.setVisibility(View.VISIBLE);
				image_4.setVisibility(View.VISIBLE);
				image_5.setVisibility(View.VISIBLE);
				image_6.setVisibility(View.VISIBLE);
				image_7.setVisibility(View.VISIBLE);
				image_8.setVisibility(View.INVISIBLE);
				image_9.setVisibility(View.INVISIBLE);
				MyUtils.loadImg(strs[0], image_1);
				MyUtils.loadImg(strs[1], image_2);
				MyUtils.loadImg(strs[2], image_3);
				MyUtils.loadImg(strs[3], image_4);
				MyUtils.loadImg(strs[4], image_5);
				MyUtils.loadImg(strs[5], image_6);
				MyUtils.loadImg(strs[6], image_7);
				break;
			case 8:
				ll_image_1.setVisibility(View.VISIBLE);
				ll_image_2.setVisibility(View.VISIBLE);
				ll_image_3.setVisibility(View.VISIBLE);
				image_1.setVisibility(View.VISIBLE);
				image_2.setVisibility(View.VISIBLE);
				image_3.setVisibility(View.VISIBLE);
				image_4.setVisibility(View.VISIBLE);
				image_5.setVisibility(View.VISIBLE);
				image_6.setVisibility(View.VISIBLE);
				image_7.setVisibility(View.VISIBLE);
				image_8.setVisibility(View.VISIBLE);
				image_9.setVisibility(View.INVISIBLE);
				MyUtils.loadImg(strs[0], image_1);
				MyUtils.loadImg(strs[1], image_2);
				MyUtils.loadImg(strs[2], image_3);
				MyUtils.loadImg(strs[3], image_4);
				MyUtils.loadImg(strs[4], image_5);
				MyUtils.loadImg(strs[5], image_6);
				MyUtils.loadImg(strs[6], image_7);
				MyUtils.loadImg(strs[7], image_8);
				break;
			case 9:
				ll_image_1.setVisibility(View.VISIBLE);
				ll_image_2.setVisibility(View.VISIBLE);
				ll_image_3.setVisibility(View.VISIBLE);
				image_1.setVisibility(View.VISIBLE);
				image_2.setVisibility(View.VISIBLE);
				image_3.setVisibility(View.VISIBLE);
				image_4.setVisibility(View.VISIBLE);
				image_5.setVisibility(View.VISIBLE);
				image_6.setVisibility(View.VISIBLE);
				image_7.setVisibility(View.VISIBLE);
				image_8.setVisibility(View.VISIBLE);
				image_9.setVisibility(View.VISIBLE);
				MyUtils.loadImg(strs[0], image_1);
				MyUtils.loadImg(strs[1], image_2);
				MyUtils.loadImg(strs[2], image_3);
				MyUtils.loadImg(strs[3], image_4);
				MyUtils.loadImg(strs[4], image_5);
				MyUtils.loadImg(strs[5], image_6);
				MyUtils.loadImg(strs[6], image_7);
				MyUtils.loadImg(strs[7], image_8);
				MyUtils.loadImg(strs[8], image_9);
				break;

			default:
				break;
			}
		}
	}

	@Override
	protected void findViewById() {
		// TODO Auto-generated method stub
		setContentView(R.layout.activity_fans_circle);
		iv_back = (ImageView) findViewById(R.id.iv_back);
		tv_title = (TextView) findViewById(R.id.tv_title);
		listView = (ListView) findViewById(R.id.listView);
		
		ll_onlive = (LinearLayout) findViewById(R.id.ll_onlive);
		tv_name = (TextView) findViewById(R.id.tv_name);
		tv_time = (TextView) findViewById(R.id.tv_time);
		tv_content = (TextView) findViewById(R.id.tv_content);
		tv_like_num = (TextView) findViewById(R.id.tv_like_num);
		tv_comment_num = (TextView) findViewById(R.id.tv_comment_num);
		tv_share_num = (TextView) findViewById(R.id.tv_share_num);
		ll_image_1 = (LinearLayout) findViewById(R.id.ll_image_1);
		ll_image_2 = (LinearLayout) findViewById(R.id.ll_image_2);
		ll_image_3 = (LinearLayout) findViewById(R.id.ll_image_3);
		image_1 = (ImageView) findViewById(R.id.image_1);
		image_2 = (ImageView) findViewById(R.id.image_2);
		image_3= (ImageView) findViewById(R.id.image_3);
		image_4 = (ImageView) findViewById(R.id.image_4);
		image_5 = (ImageView) findViewById(R.id.image_5);
		image_6 = (ImageView) findViewById(R.id.image_6);
		image_7 = (ImageView) findViewById(R.id.image_7);
		image_8 = (ImageView) findViewById(R.id.image_8);
		image_9 = (ImageView) findViewById(R.id.image_9);
		iv_header = (CircleImageView) findViewById(R.id.iv_header);
		et_content = (EditText) findViewById(R.id.et_content);
		tv_send = (TextView) findViewById(R.id.tv_send);
		ll_like = (LinearLayout) findViewById(R.id.ll_like);
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		iv_back.setOnClickListener(this);
		tv_send.setOnClickListener(this);
		ll_like.setOnClickListener(this);
		tv_title.setText("详情");
		adapter = new FansCircleMsgLvAdapter(context);
		listView.setAdapter(adapter);
		circleData = (StarCircleData) getIntent().getSerializableExtra("data");
		if (null!=circleData) {
			setData();
			getCircleMsgList();
		}
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				FansCircleMsgData clickmsgData = (FansCircleMsgData) arg0.getAdapter().getItem(arg2);
				clickMsgCid = clickmsgData.getCid();
				clickMsgCudid = clickmsgData.getC_udid();
				clickMsgCnickname = clickmsgData.getU_nickname();
				et_content.setText("回复"+clickMsgCnickname+":");
				et_content.setSelection(et_content.getText().length());
			}
		});
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		super.onClick(v);
		switch (v.getId()) {
		case R.id.iv_back:
			this.finish();
			break;
		case R.id.tv_send:
			String str_send = et_content.getText().toString().trim();
			if (TextUtils.isEmpty(str_send)) {
				showToast("请输入评论内容!");
				return;
			}
			if (!TextUtils.isEmpty(clickMsgCid)&&!TextUtils.isEmpty(clickMsgCudid)&&!TextUtils.isEmpty(clickMsgCnickname)) {
				if (str_send.startsWith("回复"+clickMsgCnickname+":")) {
					String strs[] = str_send.split("回复"+clickMsgCnickname+":");
					sendContent(strs[1],clickMsgCid,clickMsgCudid);
				}else {
					sendContent(str_send,"","");
				}
			}else {
				sendContent(str_send,"","");
			}
			break;
		case R.id.ll_like:
			btnLike();
			break;
		default:
			break;
		}
	}

}
